/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.common;

import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.core.data.DataUtils;
import com.inspur.edp.cef.designtime.api.element.GspElementObjectType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.InfixExpression;
import org.eclipse.jdt.core.dom.InfixExpression.Operator;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
import org.eclipse.jdt.core.dom.ReturnStatement;

public class DataStaticEqualsMethodGenerator extends ClassMethodGenerator {

  private final List<MediatePropertyInfo> propertyInfos;
  private final String className;
  private final String targetInterfaceType;
  private  final String paramSource="source";
  private  final String paramTarget="target";

  public DataStaticEqualsMethodGenerator(List<MediatePropertyInfo> propertyInfos, String className,
      String targetInterfaceType)
  {
    Objects.requireNonNull(propertyInfos);
    this.propertyInfos = propertyInfos.stream().filter(item -> item.getElement() != null).collect(
            Collectors.toList());
//    this.propertyInfos = propertyInfos;
    this.className = className;
    this.targetInterfaceType = targetInterfaceType;
  }
  @Override
  protected Block buildMethodBody() {
    Block block=ast.newBlock();
    ReturnStatement returnStatement=ast.newReturnStatement();
    addProperties(returnStatement);
    block.statements().add(returnStatement);
    return block;
  }

  private void addProperties(ReturnStatement returnStatement) {
    if(propertyInfos==null || propertyInfos.isEmpty()){
      returnStatement.setExpression(ast.newBooleanLiteral(true));
      return;
    }

    if(propertyInfos.size()==1)
    {
      MethodInvocation methodInvocation=getMethodInvocation(propertyInfos.get(0));
      returnStatement.setExpression(methodInvocation);
      return;
    }

    InfixExpression infixExpressionFirst=null;
    InfixExpression infixExpression=null;
    for (int i=0;i<propertyInfos.size();i++)
    {
//      if(propertyInfos.get(i).getElement() == null){
//        continue;
//      }
      if(i==propertyInfos.size()-1)
      {
        infixExpression.setRightOperand(getMethodInvocation(propertyInfos.get(i)));
      }
      else
      {
        InfixExpression infixExpression1=ast.newInfixExpression();
        infixExpression1.setOperator(Operator.CONDITIONAL_AND);
        infixExpression1.setLeftOperand(getMethodInvocation(propertyInfos.get(i)));
        if(infixExpressionFirst == null)
        {infixExpressionFirst=infixExpression1;}
        if(infixExpression!=null)
          infixExpression.setRightOperand(infixExpression1);
        infixExpression=infixExpression1;
      }
    }

    returnStatement.setExpression(infixExpressionFirst);
  }

  private MethodInvocation getMethodInvocation(MediatePropertyInfo propertyInfo) {
   MethodInvocation methodInvocation=ast.newMethodInvocation();
   String methodName=getEqualMethodName(propertyInfo);
   addImport(DataUtils.class.getTypeName());
   methodInvocation.setExpression(ast.newSimpleName(DataUtils.class.getSimpleName()));
   methodInvocation.setName(ast.newSimpleName(methodName));

   String getMethodName="get"+propertyInfo.getElement().getLabelID();

   MethodInvocation methodInvocationSource=ast.newMethodInvocation();
   methodInvocationSource.setExpression(ast.newSimpleName(paramSource));
    methodInvocationSource.setName(ast.newSimpleName(getMethodName));
   methodInvocation.arguments().add(methodInvocationSource);

    MethodInvocation methodInvocationTarget=ast.newMethodInvocation();
    methodInvocationTarget.setExpression(ast.newSimpleName(paramTarget));
    methodInvocationTarget.setName(ast.newSimpleName(getMethodName));
    methodInvocation.arguments().add(methodInvocationTarget);

   return methodInvocation;
  }

  private String getEqualMethodName(MediatePropertyInfo propertyInfo) {
    if(propertyInfo.getElement().getIsUdt())
      return "isUdtEquals";
    if(propertyInfo.getElement().getObjectType()== GspElementObjectType.Association)
      return "isAssoValueEqueals";
    if(propertyInfo.getElement().getObjectType()==GspElementObjectType.Enum)
    return "isEnumEqueals";
    if(propertyInfo.getElement().getObjectType()==GspElementObjectType.DynamicProp)
    return "isDynamicEqueals";

    switch (propertyInfo.getElement().getMDataType())
    {
      case String:
      case Text:
        return "isStringEquals";
      case Integer:
        return "isIntEquals";
      case Decimal:
       return "isDecimalEquals";
      case Boolean:
       return "isBooleanEquals";
      case Date:
      case DateTime:
       return "isDateEqueals";
      case Binary:
        return "isBytesEqueals";
    }
    throw new RuntimeException("未识别的类型"+propertyInfo.getElement().getName());
  }

  @Override
  protected String getMethodName() {
    return "equals";
  }

  @Override
  protected TypeInfo getReturnType() {
    return new TypeInfo("boolean");
  }

  @Override
  protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
    return new ArrayList<>(
        Arrays.asList(new Modifier.ModifierKeyword[]{Modifier.ModifierKeyword.PUBLIC_KEYWORD,ModifierKeyword.STATIC_KEYWORD}));
  }

  @Override
  protected ArrayList<ParameterInfo> getParameterCollection() {
    ArrayList<ParameterInfo> list =new ArrayList<>();
    ParameterInfo parameterInfoSource = new ParameterInfo();
    parameterInfoSource.setParamName(paramSource);
    TypeInfo typeInfoSource = new TypeInfo(targetInterfaceType);
    typeInfoSource.setUseFullName(true);

    parameterInfoSource.setParamType(typeInfoSource);
    list.add(parameterInfoSource);

    ParameterInfo parameterInfoTarget = new ParameterInfo();
    parameterInfoTarget.setParamName(paramTarget);
    TypeInfo typeInfoTarget = new TypeInfo(targetInterfaceType);
    typeInfoTarget.setUseFullName(true);

    parameterInfoTarget.setParamType(typeInfoTarget);
    list.add(parameterInfoTarget);
    return  list;
  }
}
